package cn.com.w.web.controller;

import cn.com.w.common.VO.BaseVO;
import cn.com.w.dao.Entity.TType;
import cn.com.w.dao.Service.ITTypeService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/type")
@Api(tags = "博客类型维护")
public class TypeController {
    private final ITTypeService tTypeService;

    public TypeController(ITTypeService tTypeService) {
        this.tTypeService = tTypeService;
    }

    @RequestMapping("/list")
    public BaseVO<?> listType() {
        return BaseVO.builder()
                .code(200)
                .content(tTypeService.list())
                .build();
    }

    @RequestMapping("/add")
    public BaseVO<Boolean> addType(@RequestParam String name) {
        return BaseVO.<Boolean>builder()
                .code(200)
                .content(tTypeService.save(TType.builder().name(name).build()))
                .build();
    }

    @RequestMapping("/delete")
    public BaseVO<Boolean> deleteType(@RequestParam Long id) {
        return BaseVO.<Boolean>builder()
                .code(200)
                .content(tTypeService.removeById(id))
                .build();
    }

    @RequestMapping("/update")
    public BaseVO<Boolean> updateType(@RequestParam Long id, @RequestParam String name) {
        return BaseVO.<Boolean>builder()
                .code(200)
                .content(tTypeService.updateById(TType.builder().id(id).name(name).build()))
                .build();
    }
}
